package com.jsmtr.rms.controller;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.jsmtr.rms.entity.*;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.jsmtr.rms.service.INZService;
import com.jsmtr.rms.service.IUtilService;


@Controller
@RequestMapping("/Do/NZApply")
public class DoNZApplyController {

    @Resource
    private IUtilService utilService;

    @Resource
    private INZService nzService;


    //[start] 查询应聘者信息

    //首页
    @RequestMapping("/")
    public String Index(HttpServletRequest request, Model model) {
        try {
            model.addAttribute("path", request.getContextPath());
            String pid = request.getParameter("pid").trim();
            String fid = request.getParameter("fid").trim();
            String deptcode = request.getParameter("deptcode").trim();

            model.addAttribute("pid", pid);
            model.addAttribute("fid", fid);
            model.addAttribute("deptcode", deptcode);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "Do/Internal/Apply/index";
    }

    @SuppressWarnings("unchecked")
    @RequestMapping("/getusers")
    public void getusers(HttpServletRequest request, HttpServletResponse response, DUserResume user) {
        try {
            int perpage = Integer.parseInt(request.getParameter("perpage"));
            int page = Integer.parseInt(request.getParameter("page"));
            String flowid = request.getParameter("flowid").trim();
            user.setPath("/" + flowid + "/");

            Map<String, Object> map = nzService.getUsers(page, perpage, user);
            StringBuilder result = new StringBuilder();
            double pageCount = (Double) map.get("pageCount");
            double allcount = (Double) map.get("count");

            if (pageCount != 0) {
                Object object = map.get("list");
                int num = 0;
                for (DNZResume _user : (List<DNZResume>) object) {
                    result.append("<tr><td><input type='checkbox' id='cb_" + _user.getId() + "'></td>");
                    result.append("<td>" + (perpage * (page - 1) + num + 1) + "</td>");
                    result.append("<td>" + _user.getCode() + "</td>");
                    result.append("<td class='link'><a target='_blank' href='"
                            + request.getContextPath() + "/Do/NZApply/View?id="
                            + _user.getId() + "'>" + _user.getName()
                            + "</a></td>");
                    result.append("<td>" + _user.getSexname() + "</td>");
                    result.append("<td>" + _user.getAge() + "</td>");
                    result.append("<td>" + _user.getZzmmname() + "</td>");
                    result.append("<td>" + _user.getBirthday() + "</td>");
                    result.append("<td>" + _user.getGroupdate() + "</td>");
                    result.append("<td>" + _user.getEducation() + "</td>");
                    result.append("<td>" + _user.getTel() + "</td>");

                    if (_user.getFid().equals(flowid)) {
                        result.append("<td class='true'><span>未完成</td></tr>");
                    } else {
                        result.append("<td class='false'><span>已完成</td></tr>");
                    }

                    num++;
                }
            } else {
                result.append("<tr style='display:none'><td></td></tr>");
            }
            result.append("<input type='hidden' id='pageCount' value='" + pageCount + "' />");
            result.append("<input type='hidden' id='allCount' value='" + allcount + "' />");
            utilService.printTextResult(result.toString(), response);
        } catch (Exception e) {
            e.printStackTrace();
            utilService.printTextResult("<tr><td colspan='11'>读取数据失败</td></tr><input type='hidden' id='pageCount' value='0' /><input type='hidden' id='allCount' value='0' />", response);
        }

    }

    @RequestMapping("/View")
    public String View(HttpServletRequest request, Model model) {
        try {
            String id = request.getParameter("id").trim();
            DNZResume resume = nzService.getDResume(id);
            model.addAttribute("name", resume.getName());
            StringBuilder str = new StringBuilder();
            str.append("<tr><td width='20' rowspan='4' class='title'><b>基本信息</b></td>");
            str.append("<td width='90'>员工号:</td><td width='170'>" + resume.getCode() + "</td>");
            str.append("<td width='70'>姓名:</td><td width='170'>" + resume.getName() + "</td>");
            str.append("<td width='70'>性别:</td><td>" + resume.getSexname() + "</td></tr>");
            str.append("<tr><td>出生日期:</td><td>" + resume.getBirthday() + "</td>");
            str.append("<td>年龄:</td><td>" + resume.getAge() + "</td>");
            str.append("<td>政治面貌:</td><td>" + resume.getZzmmname() + "</td></tr>");
            str.append("<tr><td>进入集团日期:</td><td>" + resume.getGroupdate() + "</td>");
            str.append("<td>工作部门:</td><td>" + resume.getDept1() + "</td>");
            str.append("<td>工作岗位:</td><td>" + resume.getJob() + "</td></tr>");
            str.append("<tr><td>专业技术职务:</td><td>" + resume.getPosition() + "</td>");
            str.append("<td>技能等级:</td><td>" + resume.getJndj() + "</td>");
            str.append("<td>联系电话:</td><td>" + resume.getTel() + "</td></tr>");
            model.addAttribute("resume", str);

            //教育背景
            StringBuilder str1 = new StringBuilder();
            int ecount = 1;
            List<DNZResumeEducation> educations = nzService.getEducationByRid(id);
            for (DNZResumeEducation education : educations) {
                str1.append("<tr><td>" + education.getXxmc() + "</td>");
                str1.append("<td>" + education.getMajor() + "</td>");
                str1.append("<td>" + education.getEducation() + "</td></tr>");
                ecount++;
            }
            model.addAttribute("education", str1);
            model.addAttribute("ecount", ecount);

            //工作履历
            StringBuilder str2 = new StringBuilder();
            List<DNZResumeWork> works = nzService.getWorkByRid(id);
            for (DNZResumeWork work : works) {
                str2.append("<table class='treeTable' style='margin-top:5px;'><tbody>");
                str2.append("<tr><td width='20' rowspan='13' class='title'><b>工作履历</b></td>");
                str2.append("<td>工作单位:</td><td colspan='5'>" + work.getCompany() + "</td></tr>");
                str2.append("<tr><td width='70'>工作部门:</td><td width='135'>" + work.getDept() + "</td>");
                str2.append("<td width='70'>工作岗位:</td><td width='130'>" + work.getJob() + "</td>");
                str2.append("<td width='70'>起止日期:</td><td>" + work.getStartdate() + " - " + work.getEnddate() + "</td></tr>");
                str2.append("<tr><td>工作内容:</td><td colspan='5'>" + work.getWorkcont() + "</td></tr>");
                str2.append("<tr><td>工作职责:</td><td colspan='5'>" + work.getWorkzz() + "</td></tr>");
                str2.append("<tr><td>工作经历<br>工作经验:</td><td colspan='5'>");
                str2.append((work.getWorkremark() == null ? "" : work.getWorkremark()) + "</td>");
                str2.append("</tr></tbody></table>");
            }
            model.addAttribute("work", str2);

        } catch (Exception e) {
            e.printStackTrace();
        }
        return "Do/Internal/Apply/view";
    }

    //[end]

    //[start] 开启关闭简历投递功能

    @RequestMapping(value = "/OpenCloseAction", method = RequestMethod.POST)
    public void OpenCloseAction(HttpServletRequest request, HttpServletResponse response) {
        try {
            //String pid = request.getParameter("pid").trim();//招聘方案ID
            String fid = request.getParameter("fid").trim();//招聘流程ID
            String deptcode = request.getParameter("deptcode").trim();//责任部门编码
            int state = Integer.parseInt(request.getParameter("state").trim());
            BaseUser usr = utilService.getSessionUser(request);
            if (usr.getUdept().equals(deptcode)) {
                ProjectInfoFlow flow = nzService.getFlow(fid);
                flow.setState(state);
                utilService.editObject(flow);
                utilService.printJsonResult(true, "操作成功", "no", response);
            } else {
                utilService.printJsonResult(false, "您无权执行此操作！", "no", response);
            }
        } catch (Exception e) {
            e.printStackTrace();
            utilService.printJsonResult(false, "操作失败", "no", response);
        }
    }

    //[end]
}
